﻿using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;

namespace ChatClientApp.Tools
{
    public static class Tools
    {
        private static IPAddress server_ip = null; 
        public static string NL = System.Environment.NewLine;

        public static string HOSTNAME = "Berdsen-Netbook";

        private static int SERVER_PORT = 1500;

        public static IPEndPoint SERVER_ENDPOINT = new IPEndPoint(SERVER_IP, SERVER_PORT);

        private static IPAddress SERVER_IP
        {
            get {
                try
                {
                    if (server_ip == null)
                        return IPAddress.Loopback;
                    else
                        return server_ip;
                    /*
                    IPAddress[] adresslist = Dns.GetHostAddresses(HOSTNAME);
                    foreach (IPAddress ipa in adresslist)
                    {
                        if (ipa.AddressFamily == AddressFamily.InterNetwork)
                        {
                            return ipa;
                        }
                    }
                    return IPAddress.Loopback;
                    */
                }
                catch
                {
                    return IPAddress.Loopback;
                }
            }
            set
            {
                try
                {
                    if (value == null)
                    {
                        IPAddress[] adresslist = Dns.GetHostAddresses(HOSTNAME);
                        foreach (IPAddress ipa in adresslist)
                        {
                            if (ipa.AddressFamily == AddressFamily.InterNetwork)
                            {
                                server_ip = ipa;
                            }
                        }
                    }
                    else
                    {
                        server_ip = value;
                    }

                }
                catch
                {
                    server_ip = null;
                }
            }
        }

        public static byte[] ToAsciiByteArray(this string s)
        {
            var asciiEncoding = new ASCIIEncoding();
            return asciiEncoding.GetBytes(s);
        }

        public static byte[] ToByteArray(this string s)
        {
            var enc = Encoding.GetEncoding(0);
            return enc.GetBytes(s);
        }

        public static string ToAsciiString(this byte[] b)
        {
            var asciiEncoding = new ASCIIEncoding();
            return asciiEncoding.GetString(b);
        }

        public static string[] ToAsciiString(this byte[] b, string sep)
        {
            string[] array = new string[] { sep };
            var asciiEncoding = new ASCIIEncoding();
            return asciiEncoding.GetString(b).Split(array, StringSplitOptions.None);
        }

        public static string[] ToAsciiString(this string s, string sep)
        {
            string[] array = new string[] { sep };
            return s.Split(array, StringSplitOptions.None);
        }

        public static string ToConcatedString(this string[] s)
        {
            string ret = "";
            foreach (string element in s)
                ret += element + "; ";

            ret = ret.Substring(0, ret.LastIndexOf(";"));

            return ret; 
        }

        public static Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            if (destWidth == 0)
                destWidth = 1;
            if (destHeight == 0)
                destHeight = 1;


            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }

        public static void UpdateServer()
        {
            string[] config = System.IO.File.ReadAllLines("config.cfg");
            foreach (string element in config)
            {
                if (element.ToLower().Contains("server_hostname"))
                {
                    try
                    {
                        HOSTNAME = element.Substring(element.IndexOf("=") + 1).Trim();
                        SERVER_IP = null;
                    }
                    catch
                    {
                    }
                }

                if (element.ToLower().Contains("server_port"))
                {
                    try
                    {
                        SERVER_PORT = Convert.ToInt32(element.Substring(element.IndexOf("=") + 1).Trim());
                    }
                    catch
                    {

                    }
                }

                if (element.ToLower().Contains("server_ip"))
                {
                    try
                    {
                        string ip = element.Substring(element.IndexOf("=") + 1).Trim();
                        IPAddress ipa;
                        if (IPAddress.TryParse(ip, out ipa))
                        {
                            server_ip = ipa;
                        }
                    }
                    catch
                    {

                    }
                }

            }

            SERVER_ENDPOINT = new IPEndPoint(SERVER_IP, SERVER_PORT);
        }
    }
}
